<template>
	<div>
		<el-form :inline="true">
			<el-form-item>
				<el-input
						v-model="searchForm.context"
						placeholder="用户名"
						clearable
				>
				</el-input>
			</el-form-item>

			<el-form-item>
				<el-button @click="getUserList">搜索</el-button>
			</el-form-item>

			<el-form-item>
				<el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
					<el-button type="danger" slot="reference" :disabled="delBtlState" v-if="hasAuth('sys:user:delete')">批量删除</el-button>
				</el-popconfirm>
			</el-form-item>
		</el-form>

		<el-table
				ref="multipleTable"
				:data="tableData"
				tooltip-effect="dark"
				style="width: 100%"
				border
				stripe
				@selection-change="handleSelectionChange">

			<el-table-column
					type="selection"
					width="55">
			</el-table-column>


			<el-table-column
					prop="title"
					label="标题"
					width="120">
			</el-table-column>

			<el-table-column
					prop="title"
					label="用户"
					width="120">
			</el-table-column>

			<el-table-column
					prop="carId"
					label="汽车品牌"
					width="120">
					<template slot-scope="scope">
						<p  v-if="scope.row.brandId === 0" >奔驰</p>
						<p  v-if="scope.row.brandId === 1" >宝马</p>
						<p  v-if="scope.row.brandId === 2" >法拉利</p>
					</template>
					
			</el-table-column>

            <el-table-column
					prop="issueTime"
					label="发行时间"
					width="120">
			</el-table-column>

            <el-table-column
					prop="hot"
					label="热度"
					width="120">
			</el-table-column>

			

			<el-table-column
					prop="state"
					label="状态">
				<template slot-scope="scope">
					<el-tag size="small" v-if="scope.row.state === 1" type="success">已通过</el-tag>
					<el-tag size="small" v-else-if="scope.row.state === 0" type="warning">待审核</el-tag>
					<el-tag size="small" v-else-if="scope.row.state === 2" type="danger">未通过</el-tag>
				</template>

			</el-table-column>
			<el-table-column
					prop="icon"
					width="260px"
					label="操作">

				<template slot-scope="scope">
					<el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>
					<el-divider direction="vertical"></el-divider>

					<template>
						<el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
							<el-button type="text" slot="reference">删除</el-button>
						</el-popconfirm>
					</template>

				</template>
			</el-table-column>

		</el-table>

		<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
				:page-sizes="[10, 20, 50, 100]"
				:current-page="current"
				:page-size="size"
				:total="total">
		</el-pagination>


		<!--新增对话框-->
		<el-dialog
				title="提示"
				:visible.sync="dialogVisible"
				width="600px"
				:before-close="handleClose">

			<el-form :model="editForm" :rules="editFormRules" ref="editForm" >
				<el-form-item label="标题" prop="title" label-width="100px">
					<el-input v-model="editForm.title" autocomplete="off"></el-input>
				</el-form-item>

				<!-- <el-form-item label="发行时间" prop="issueTime" label-width="100px">
					<el-input v-model="editForm.issueTime" autocomplete="off"></el-input>
					<el-date-picker
						v-model="editForm.issueTime"
						type="datetime"
						placeholder="选择日期时间">
					</el-date-picker>
				</el-form-item> -->

				<el-form-item label="热度"  prop="hot"  label-width="100px">
					<el-input v-model="editForm.hot" type="number" autocomplete="off"></el-input>
				</el-form-item>

				<el-form-item label="状态"  prop="state" label-width="100px">
					<el-radio-group v-model="editForm.state">
						<el-radio :label="0">待审核</el-radio>
						<el-radio :label="1">通过</el-radio>
						<el-radio :label="2">未通过</el-radio>
					</el-radio-group>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="resetForm('editForm')">取 消</el-button>
				<el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 分配权限对话框 -->
	</div>
</template>

<script>
	export default {
		name: "Manage",
		data() {
			var validHot = (rule, value, callback) => {
				if (value < 0) {
				callback(new Error('请输入正确热度'));
				}
				callback();
			};
			return {
				searchForm: {},
				delBtlState: true,

				total: 0,
				size: 10,
				current: 1,

				dialogVisible: false,
				editForm: {
				},

				tableData: [],

				editFormRules: {
					title: [
						{required: true, message: '请输入标题', trigger: 'blur'}
					],
					hot: [
						{validator: validHot, trigger: 'blur'}
					]
				},

				multipleSelection: [],

			}
		},
		created() {
			this.getUserList()

			
		},
		methods: {
			toggleSelection(rows) {
				if (rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row);
					});
				} else {
					this.$refs.multipleTable.clearSelection();
				}
			},
			handleSelectionChange(val) {
				console.log("勾选")
				console.log(val)
				this.multipleSelection = val;

				this.delBtlState = val.length == 0
			},

			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.size = val
				this.getUserList()
			},
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.current = val
				this.getUserList()
			},

			resetForm(formName) {
				this.$refs[formName].resetFields();
				this.dialogVisible = false
				this.editForm = {}
			},
			handleClose() {
				this.resetForm('editForm')
			},

			getUserList() {
				this.$axios.get("/article/page1", {
					params: {
						context: this.searchForm.context,
						pageNum: this.current,
						pageSize: this.size
					}
				}).then(res => {
					this.tableData = res.data.data.records
					this.size = res.data.data.size
					this.current = res.data.data.current
					this.total = res.data.data.total
				})
			},

			
			editHandle(id) {
				this.$axios.get('/article/' + id).then(res => {
					this.editForm = res.data.data
					console.log(this.editForm.client.username)
					this.dialogVisible = true
				})
			},
			delHandle(id) {

				var ids = []

				if (id) {
					ids.push(id)
				} else {
					this.multipleSelection.forEach(row => {
						ids.push(row.id)
					})
				}

				console.log(ids)

				this.$axios.post("/article/delete", ids).then(res => {
					this.$message({
						showClose: true,
						message: '恭喜你，操作成功',
						type: 'success',
						onClose:() => {
							this.getUserList()
						}
					});
				})
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
				if (valid) {

					this.$axios.post("/article",this.editForm).then(res =>{
						this.dialogVisible = false
						this.getUserList()
					})
				} else {
					console.log('error submit!!');
					return false;
				}
				});
			},

		}
	}
</script>

<style scoped>

	.el-pagination {
		float: right;
		margin-top: 22px;
	}

</style>